import REQ, {ResData} from "@/utils/ApiUtil.ts";
import {IPage, ObjInfo, ObjType} from "@/api/Types/ObjInterfaces.ts";

class ObjApi {
    public getObjTypes = async () => {
        return await REQ.get<null, ResData<ObjType[]>>("api/obj/types").then(data => {
            return data.data
        })
    }

    public pageAllObj = async (currentPage: number, pageSize: number, type?: string) => {
        const res = await REQ<null, ResData<IPage<ObjInfo>>>({
            method: 'get',
            url: "/api/obj/page/all",
            params: {
                current: currentPage,
                size: pageSize,
                type: type,
            },
        })
        return res.data
    }

    public getUserObj = async (currentPage: number, pageSize: number, type?: string) => {
        const res = await REQ<null, ResData<IPage<ObjInfo>>>({
            method: 'get',
            url: "/api/obj/page",
            params: {
                current: currentPage,
                size: pageSize,
                type: type,
            },
        })
        return res.data
    }

    public deleteObj = async (objId: string) => {
        return await REQ({
            method: 'get',
            url: '/api/obj/delete/' + objId
        })
    }

    public changeObjName = async (objId: string, name: string) => {
        return await REQ<ObjInfo, ResData<void>>({
            method: 'post',
            url: 'api/obj/update',
            data: {
                id: objId,
                objName: name
            }
        })
    }

    public getObjLink = (objLink: string) => {
        return "api/obj/" + objLink
    }

    public changeObjType = async (objId: string, objType: string) => {
        return await REQ<ObjInfo, ResData<void>>({
            method: 'post',
            url: 'api/obj/update',
            data: {
                id: objId,
                objType: {
                    id: objType
                }
            }
        })
    }
}

export default new ObjApi()